<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_megaphone extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize(){
		 require_once('engine/class.www-database.php');
		$this->conn=new WWW_Database();				
		
		require_once('engine/include_dao.php');
		return true;
		
	}
	public function create_megaphone($data)
	{
			$file = $data['megaphone_image']['tmp_name'];
			$filename = time().$data["megaphone_image"]['name'];
			$megaphone_image_path="uploads/megaphone/".$filename;
			move_uploaded_file($file, $banner_image_path);
		
		$megaphone = new Megaphone();
		$megaphone->megaphone_title = $data['megaphone_title'];
		$megaphone->megaphone_displayorder = $data['megaphone_displayorder'];
		$megaphone->megaphone_starton = $data['megaphone_starton'];
		$megaphone->megaphone_expireon = $data['megaphone_expireon'];
		$megaphone->megaphone_content = $data['megaphone_content'];
		$megaphone->megaphone_image = $this->conn->baseurl().$megaphone_image_path;
		$megaphone->created_date = date("Y-m-d H:i:s");
		$megaphone->updated_date = date("Y-m-d H:i:s");
		$result = DAOFactory::getMegaphoneDAO()->insert($megaphone);
		//print_r($result);exit;
		
	/* $query="insert into `megaphone` (megaphone_title,megaphone_displayorder,megaphone_starton,megaphone_expireon,megaphone_content,megaphone_image,created_date,updated_date) VALUES ('".$data['megaphone_title']."','".$data['megaphone_displayorder']."','".date($data['megaphone_starton'])."','".date($data['megaphone_expireon'])."','".$data['megaphone_content']."','".$this->conn->baseurl().$megaphone_image_path."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
	//print_r($query);exit;
	$result=$this->conn->dbCommand($query);	 */
		if($result !== ''){
			return array('message'=>'Megaphone successfully created');			
		}else{
			return array('message'=>'Megaphone Creation Failed');
		}	
	}
	public function edit_megaphone($data)
	{
		if($data['megaphone_image']!="")
			{
			$file = $data['megaphone_image']['tmp_name'];
			$filename = time().$data["megaphone_image"]['name'];
			$megaphone_image_path="uploads/megaphone/".$filename;
			move_uploaded_file($file, $banner_image_path);
			}
			else
			{
				$megaphone_image_path = "";
			}
			
			$megaphone = new Megaphone();
		$megaphone->megaphone_title = $data['megaphone_title'];
		$megaphone->megaphone_displayorder = $data['megaphone_displayorder'];
		$megaphone->megaphone_starton = $data['megaphone_starton'];
		$megaphone->megaphone_expireon = $data['megaphone_expireon'];
		$megaphone->megaphone_content = $data['megaphone_content'];
		$megaphone->megaphone_image = $this->conn->baseurl().$megaphone_image_path;
		$megaphone->created_date = date("Y-m-d H:i:s");
		$megaphone->updated_date = date("Y-m-d H:i:s");
		$megaphone->megaphone_id = $data['megaphone_id'];
		$megaphone->status = $data['status'];
		$result = DAOFactory::getMegaphoneDAO()->update($megaphone);
		//print_r($result);exit;
		
		
	/* $query="update `megaphone` set megaphone_title='".$data['megaphone_title']."',megaphone_displayorder='".$data['megaphone_displayorder']."',megaphone_starton='".$data['megaphone_starton']."',megaphone_expireon='".$data['megaphone_expireon']."',megaphone_content='".$data['megaphone_content']."'".($megaphone_image_path!="" ? ',banner_image='.$this->conn->baseurl().$megaphone_image_path : '').",updated_date='".date("Y-m-d H:i:s")."' where megaphone_id=".$data['megaphone_id'];
		//print_r($query);exit;
		$result=$this->conn->dbCommand($query);		 */
		if($result !== ''){
			return array('message'=>'Megaphone Updated Successfully');			
		}else{
			return array('message'=>'Megaphone Updates Failed');
		}	
	
	}
	public function get_megaphone($data)
	{	
		$megaphone = new Megaphone();
		$megaphone->megaphone_id = $data['megaphone_id'];
		$result = DAOFactory::getMegaphoneDAO()->load($megaphone);
		//print_r($result);exit;
	
		 /* $query="SELECT megaphone_title,megaphone_displayorder,megaphone_starton,megaphone_expireon,megaphone_content,megaphone_image from megaphone
where megaphone_id=".$data['megaphone_id'];
			
		$result=$this->conn->dbMultiple($query); */
		if($result !== ''){
			return array('message'=>'Megaphone get Successfully', 'result'=>$result);			
		}else{
			return array('message'=>'Megaphone get Failed');
		}	
		
	}
	public function delete_megaphone($data){
		$megaphone = new Megaphone();
		$megaphone->megaphone_id = $data['megaphone_id'];
		$megaphone->status = $data['status'];
		$result = DAOFactory::getMegaphoneDAO()->delete_megaphone($megaphone);
		//print_r($result);exit;
	
		/* $data=$this->conn->dbCommand("update `megaphone` set status='".$data['status']."' where megaphone_id=".$data['megaphone_id']);
 */		if($result == 1)
			return array('message'=>"Megaphone Deleted Successfully");		
		else
			return array('message'=>'Megaphone Deletion Fails');
	}
	public function all_megaphones($data){
		$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['results_per_page']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
		
	/* $query_string="select * from `megaphone`";
	$query_string1="SELECT count(*) as total from megaphone";
	 */
	 if (isset($data['search_by']) && $data['search_by']!='')
	{	
		if($data['search_by']=='status')
		{	
			$search_by = "status";
			
		}else if($data['search_by'] == 'megaphone_title')
		{
			
			$search_by = "megaphone_title";
			
		}else if($data['search_by'] == 'megaphone_starton')
		{
			$search_by = "megaphone_starton";
		}
		else if($data['search_by'] == 'megaphone_expireon')
		{
			$search_by = "megaphone_expireon";
		}
		
		$megaphone = new Megaphone();
		$megaphone->order_by = $order_by;
		$megaphone->offset = $offset;
		$megaphone->results_per_page = $results_per_page;
		$megaphone->sort_order = $sort_order;
		$megaphone->search_by = $search_by;
		$megaphone->search_by_value = $data['search_by_value'];
		$result = DAOFactory::getMegaphoneDAO()->all_megaphone($megaphone);
		$totalrows = DAOFactory::getMegaphoneDAO()->searchby_count($megaphone);
		//print_r($result);exit;
	
		
		/* $query =$query_string." where ".$search_by." like '%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
			$query1 =$query_string1." where ".$search_by." like '%".$data['search_by_value']."%'";
		 */	
	}else
	{	
		$megaphone = new Megaphone();
		$megaphone->order_by = $order_by;
		$megaphone->offset = $offset;
		$megaphone->results_per_page = $results_per_page;
		$megaphone->sort_order = $sort_order;
		$result = DAOFactory::getMegaphoneDAO()->queryAll($megaphone);
		$totalrows = DAOFactory::getMegaphoneDAO()->megaphone_count($megaphone);
		//print_r($result);exit;
	
	}
		
		if($result){
			
		//print_r($totalrows);exit;
	
		return array('message'=>"All Megaphones","info"=>$result,'total'=>$totalrows,'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No Megaphone');
	}

}
	
?>